<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'user.php');
include(mnminclude.'friend.php');
include(mnminclude.'smartyvariables.php');
include(mnminclude.'pageview.php');

// NO SIDEBAR, WIDER CONTENTBOX,NO HEADER SORTING
$main_smarty->assign('no_sidebar',true);
$main_smarty->assign('no_headsort',true);



$offset=(get_current_page()-1)*$page_size;
$main_smarty = do_sidebar($main_smarty);

define('pagename', 'user'); 
$main_smarty->assign('pagename', pagename);


// if not logged in, redirect to the index page
	if(isset($_REQUEST['login'])){$login = $_REQUEST['login'];}
	if(empty($login)){
		if ($current_user->user_id > 0) {
			$login=$current_user->user_login;
		} else {
			header('Location: ./');
			die;
		}
	}

// setup the breadcrumbs
	$navwhere['text1'] = $main_smarty->get_config_vars('PLIGG_Visual_Breadcrumb_Profile');
	$navwhere['link1'] = getmyurl('topusers');
	$navwhere['text2'] = $login;
	$navwhere['link2'] = getmyurl('user2', $login, 'profile');

// read the users information from the database
	$user=new User();
	$user->username = $login;
	if(!$user->read()) {
		echo "error 2";
		die;
	}

// find out who last viewed this users profile
	$pageview = new Pageview;
	
// insert a pageview for the current viewer
	$pageview->type='profile'; 	 
	$pageview->page_id=$user->id; 	 
	$pageview->user_id=$current_user->user_id; 	 
	require_once(mnminclude.'check_behind_proxy.php'); 	 
	$pageview->user_ip=check_ip_behind_proxy(); 	 
	$pageview->insert();	

// setup some arrays
	$last_viewers_names = array();
	$last_viewers_profile = array();
	$last_viewers_avatar = array();
		
// get the last 5 viewers
	$last_viewers = $pageview->last_viewers(5);
	
// for each viewer, get their name, profile link and avatar and put it in an array
	$viewers=new User();
	if ($last_viewers) {
		foreach($last_viewers as $viewer_id) {
			$viewers->id=$viewer_id;
			$viewers->read();
			$last_viewers_names[] = $viewers->username;
			$last_viewers_profile[] = getmyurl('user2', $viewers->username, 'profile');
			$last_viewers_avatar[] = get_avatar('small', "", $viewers->username, $viewers->email);
		}
	}
// tell smarty about our arrays
	$main_smarty->assign('last_viewers_names', $last_viewers_names);
	$main_smarty->assign('last_viewers_profile', $last_viewers_profile);
	$main_smarty->assign('last_viewers_avatar', $last_viewers_avatar);
		
// check to see if the profile is of a friend
  $friend = new Friend;
  $main_smarty->assign('is_friend', $friend->get_friend_status($user->id));


// avatars
	$main_smarty->assign('UseAvatars', do_we_use_avatars());
	$main_smarty->assign('Avatar_ImgSrc', get_avatar('large', $user->avatar, $user->username, $user->email));
	if ($user->url != "") {
		if(substr(strtoupper($user->url), 0, 7) != "HTTP://"){
			$main_smarty->assign('user_url', "http://" . $user->url);
		}
		else {
			$main_smarty->assign('user_url', $user->url);
		}
	}


// setup the URL method 2 links
	$main_smarty->assign('user_url_personal_data', getmyurl('user2', $login, 'profile'));
	$main_smarty->assign('user_url_news_sent', getmyurl('user2', $login, 'history'));
	$main_smarty->assign('user_url_news_published', getmyurl('user2', $login, 'published'));
	$main_smarty->assign('user_url_news_unpublished', getmyurl('user2', $login, 'shaken'));
	$main_smarty->assign('user_url_news_voted', getmyurl('user2', $login, 'voted'));
	$main_smarty->assign('user_url_commented', getmyurl('user2', $login, 'commented'));
	$main_smarty->assign('user_url_saved', getmyurl('user2', $login, 'saved'));
	$main_smarty->assign('user_url_friends', getmyurl('user_friends', $login, 'viewfriends'));
	$main_smarty->assign('user_url_friends2', getmyurl('user_friends', $login, 'viewfriends2'));
	$main_smarty->assign('user_url_add', getmyurl('user_add_remove', $login, 'addfriend'));
	$main_smarty->assign('user_url_remove', getmyurl('user_add_remove', $login, 'removefriend'));
	$main_smarty->assign('user_rss', getmyurl('rssuser', $login));
	$main_smarty->assign('URL_Profile', getmyurl('profile'));
  //******************************************************************************************
//User TagCloud Addon for user.php in main folder
    $main_smarty->assign('user_url_tagcloud', getmyurl('user2', $login, 'tagcloud'));                
//****************************************************************************************** 

// tell smarty about our user
	$main_smarty = $user->fill_smarty($main_smarty);


// setup breadcrumbs for the various views
	if(isset($_REQUEST['view'])){$view = $_REQUEST['view'];}
	if(empty($view)) $view = 'profile';

	$page_header = $user->username;
	$post_title = " / " . $main_smarty->get_config_vars('PLIGG_Visual_Breadcrumb_Profile') . " / " . $login;

	$main_smarty->assign('user_view', $view);
    $main_smarty->assign('user_profile_id',$user->id);
	if ($view == 'profile') {
		$main_smarty->assign('nav_pd', 4);
	} else {
		$main_smarty->assign('nav_pd', 3);
		}                                                                
 //******************************************************************************************
//User TagCloud Addon for user.php in main folder
//By AnlienHolakres3 for Pligg comunity,FREE to modify, NO WARRANTY
//********************************************************************************************
    if ($view == 'tagcloud') {
        $page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud');
        $navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud');
        $post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud');
        $main_smarty->assign('view_href', 'tagcloud');
       
       
        $word_limit = NULL; // limit to cloud to this many words
       $smarty_variable = '';
       $filterTo = 'all'; // published, queued or ALL (does not include discarded)
       $filterCategory = 0; // a specific category
       $range_values = NULL; // only used on the tagcloud page where there is a list of time ranges to filter to.
       $min_points = NULL; // the size of the smallest tag
       $max_points = NULL; // the size of the largest tag
    
   
            
        global $db, $dblang, $URLMethod, $tags_words_limit, $tags_min_pts, $tags_max_pts,$current_user, $rows, $user, $offset, $page_size;
            $user_logged_in = $current_user->user_id;
            $selected_user=$user->id; //user we want to see his own tag
            $range_values = array(0, 172800, 604800, 2592000, 31536000);   
        
        // if we didnt set a word limit, use the default set in the config.php
            if ($word_limit == NULL) {$word_limit = $tags_words_limit;}

        // if we didnt set the minimum font points, use the default set in the config.php
            if ($min_points == NULL) {$min_points = $tags_min_pts;}

        // if we didnt set the maximum font points, use the default set in the config.php
            if ($max_points == NULL) {$max_points = $tags_max_pts;}

            
        // see if we clicked on a link to filter to a specific time range
        if(($from = check_integer('range')) >= 0 && $from < count($range_values) && $range_values[$from] > 0 ) {
            $from_time = time() - $range_values[$from];
            $from_where = "FROM " . table_tags . " INNER JOIN ". table_links . " ON ". table_tags . ".tag_link_id=". table_links . ".link_id  
            WHERE tag_date > FROM_UNIXTIME($from_time) and link_author =$selected_user  ";
            
            $time_query = "&amp;from=$from_time";
            $$main_smarty->assign('time_query', $time_query);
            
        } else {
            $from_where = "FROM " . table_tags . " INNER JOIN ". table_links . " ON ". table_tags . ".tag_link_id=". table_links . ".link_id  
            WHERE link_author =$selected_user  ";
        }

        $from_where .= " GROUP BY tag_words";
              

        $max = max($db->get_var("select count(*) as words $from_where order by words desc limit 1"), 2);
        $coef = ($max_points - $min_points)/($max-1);

        $sql = "select tag_words, count(*) as count $from_where order by count desc limit $word_limit";
      //   echo $sql;
        $res = $db->get_results($sql);
        
        if ($res) {
            foreach ($res as $item) {
                
                $words[$item->tag_words] = $item->count;
            }     
            ksort($words);
             

            $tag_number = array();
            $tag_name = array();
            $tag_count = array();
            $tag_size = array();
            $tag_url = array();
            
            $tagnumber = 0;
            foreach (array_keys($words) as $theword) {
                
                $tag_number[$tagnumber] = $tagnumber;
                $tag_name[$tagnumber] = $theword;
                $tag_count[$tagnumber] = $words[$theword];
                $tag_size[$tagnumber] = $tags_min_pts + ($tag_count[$tagnumber] - 1) * $coef;
                
                if(isset($time_query)){
                    $tag_url[$tagnumber] = getmyurl('tag2', urlencode($tag_name[$tagnumber]), $from_time);
                } else {
                    $tag_url[$tagnumber] = getmyurl('tag', urlencode($tag_name[$tagnumber]));
                }
                
                $tagnumber = $tagnumber + 1;
            }
        }
        
        // Set the smarty variables for matched tags
            if(isset($words)){$main_smarty->assign('words', $words);}
            if(isset($tag_number)){$main_smarty->assign('tag_number', $tag_number);}else{$main_smarty->assign('tag_number', 0);}
            if(isset($tag_name)){$main_smarty->assign('tag_name', $tag_name);}
            if(isset($tag_count)){$main_smarty->assign('tag_count', $tag_count);}
            if(isset($tag_size)){$main_smarty->assign('tag_size', $tag_size);}
            $tag_url = str_replace(" ", "+", $tag_url); // Steef 2k7-07 tag search fix   September 07 by AshDigg
       
            
            if(isset($tag_url)){$main_smarty->assign('tag_url', $tag_url);}

           $main_smarty->assign('tags_words_limit', $word_limit);
           $main_smarty->assign('tags_min_pts', $min_points);
           $main_smarty->assign('tags_max_pts', $max_points);
           $main_smarty->assign('tags_largest_tag', $max);
           $main_smarty->assign('tags_coef', $coef);        
        
    //FOR LOGGED-IN USER
                //if is user logged in we will do some comparison with user selected tags
            $sql = "select tag_words FROM " . table_tags . " INNER JOIN " . table_links . " ON " . table_tags. ".tag_link_id=" .table_links . ".link_id
             WHERE link_author=" . $user_logged_in ." AND tag_words IN (SELECT tag_words FROM ". table_tags . " INNER JOIN " . table_links . " ON "
              . table_tags. ".tag_link_id=" .table_links . ".link_id WHERE link_author=" . $user->id .")";
            
         $res = $db->get_results($sql);      
         
          if ($res) {
            foreach ($res as $item) {
                
                $words_new[$item->tag_words] = $item->count;
            }     
            ksort($words);
             

            $tag_number = array();
            $tag_name = array();
            $tag_count = array();
            $tag_size = array();
            $tag_url = array();
            
            $tagnumber = 0;
            foreach (array_keys($words_new) as $theword_new) {
                
                $tag_number_match[$tagnumber] = $tagnumber;
                $tag_name_match[$tagnumber] = $theword_new;
                $tag_count_match[$tagnumber] = $words_new[$theword];
               
                
                $tagnumber = $tagnumber + 1;
            }
        }    
           // Set the smarty variables for matched tags
            if(isset($words)){$main_smarty->assign('tag_number_match', $tag_number_match);}
           if(isset($tag_name)){$main_smarty->assign('tag_name_match', $tag_name_match);}  
                                                                   
            
        $main_smarty->assign('nav_tc', 4);
     } else {
        $main_smarty->assign('nav_tc', 3);    
     } 
   //******************************************************************************************      
        
   
	if ($view == 'voted') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsVoted');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_NewsVoted');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsVoted');
		$main_smarty->assign('view_href', 'voted');
		$main_smarty->assign('nav_nv', 4);
	 } else {
		$main_smarty->assign('nav_nv', 3);
		}	

	if ($view == 'history') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsSent');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_NewsSent');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsSent');
		$main_smarty->assign('view_href', 'all');
		$main_smarty->assign('nav_ns', 4);
	 } else {
		$main_smarty->assign('nav_ns', 3);
		}

	if ($view == 'published') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsPublished');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_NewsPublished');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsPublished');
		$main_smarty->assign('view_href', 'published');
		$main_smarty->assign('nav_np', 4);
	 } else {
		$main_smarty->assign('nav_np', 3);
		}

	if ($view == 'shaken') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsUnPublished');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_NewsUnPublished');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsUnPublished');
		$main_smarty->assign('view_href', 'queued');
		$main_smarty->assign('nav_nu', 4);
	 } else {
		$main_smarty->assign('nav_nu', 3);
		}

	if ($view == 'commented') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsCommented');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_NewsCommented');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsCommented');
		$main_smarty->assign('view_href', 'commented');
		$main_smarty->assign('nav_c', 4);
	 } else {
		$main_smarty->assign('nav_c', 3);
		}

	if ($view == 'saved') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsSaved');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_NewsSaved');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_NewsSaved');
		$main_smarty->assign('view_href', 'saved');
		$main_smarty->assign('nav_s', 4);
	 } else {
		$main_smarty->assign('nav_s', 3);
	}	

	if ($view == 'viewfriends') {
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Viewing_Friends');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Viewing_Friends');
		}

	if ($view == 'viewfriends2') {
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Viewing_Friends_2a');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Viewing_Friends_2');
		}

	if ($view == 'removefriend') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Removing_Friend');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Removing_Friend');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Removing_Friend');
		}

	if ($view == 'addfriend') {
		$page_header .= ' / ' . $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Adding_Friend');
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Adding_Friend');
		$post_title .= " / " . $main_smarty->get_config_vars('PLIGG_Visual_User_Profile_Adding_Friend');
		}

	$main_smarty->assign('page_header', $page_header);
	$main_smarty->assign('posttitle', $post_title);

	if ($view == 'search') {
	
		if(isset($_REQUEST['keyword'])){$keyword = sanitize($_REQUEST['keyword'], 3);}
	
		$searchsql = mysql_query("SELECT * FROM " . table_users . " where user_login LIKE '%".$keyword."%' OR public_email LIKE '%".$keyword."%' OR user_date LIKE '%".$keyword."%' ");
		$userlist = array();

		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
			foreach($userlist as $key => $val){
				$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
				$userlist[$key]['add_friend'] = getmyurl('user_add_remove', $val['user_login'], 'addfriend');
				$userlist[$key]['remove_friend'] = getmyurl('user_add_remove', $val['user_login'], 'removefriend');
				$userlist[$key]['status'] = $friend->get_friend_status($val['user_id']);
			}					
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('search', $_GET['keyword']);

		$main_smarty->assign('page_header', $user->username);
		$navwhere['text3'] = $main_smarty->get_config_vars('PLIGG_Visual_Search_SearchResults') . ' ' . $_GET['keyword'];
		$main_smarty->assign('posttitle', " / " . $main_smarty->get_config_vars('PLIGG_Visual_Breadcrumb_Profile') . " / " . $login . " / " . $main_smarty->get_config_vars('PLIGG_Visual_Search_SearchResults'));
		}




	$main_smarty->assign('navbar_where', $navwhere);

 
    

// a hook	
	check_actions('user_post_views');    
    
//*******************************************************************************************
//**TagCloud Addon
//this function returns value for adding tags into tagcloud - 1 =true,0 = false
//it is used in user_center.tpl 

 function nextTag($value) {
            global $main_smarty;  
       $main_smarty->assign('next_tag',$value);     
 }

//******************************** 
    
    
// display the template
	$main_smarty->assign('tpl_center', $the_template . '/user_center');
	$main_smarty->display($the_template . '/pligg.tpl');    

//********************************************************************************************
//TagCloud AddOn - it compares user logged (if any) tags with user selected tags
//
    function do_tagcloud_compare () { 
        global $current_user,$user,$main_smarty,$db;
        $user_logged_in=$current_user->user_id;
    
        
            if ($user_logged_in!=null){
 
           $sql  = "SELECT count(tag_words) FROM (SELECT DISTINCT tag_words FROM " . table_tags . " INNER JOIN " . table_links . " ON " . table_tags. ".tag_link_id=" .table_links . ".link_id
             WHERE link_author=" . $user_logged_in ." AND tag_words IN (SELECT tag_words FROM ". table_tags . " INNER JOIN " . table_links . " ON "
              . table_tags. ".tag_link_id=" .table_links . ".link_id WHERE link_author=" . $user->id .")) AS matched_tags";
             
             $sql2  = "SELECT count(tag_words) FROM (SELECT DISTINCT tag_words FROM " . table_tags . " INNER JOIN " . table_links . " ON " . table_tags. ".tag_link_id=" .table_links . ".link_id
             WHERE link_author=" . $user_logged_in .") AS matched_tags";             
           
           $sql3 = "SELECT  count(distinct tag_words) FROM ". table_tags . " INNER JOIN " . table_links . " ON ". table_tags. ".tag_link_id=" .table_links . ".link_id WHERE link_author=" . $user->id;
             
           $matched_count = $db->get_var($sql);
           $user_logged_tags_count = $db->get_var($sql2);
           $selected_user_tags_count=$db->get_var($sql3);
          
          if ($user_logged_tags_count>0){
            $percent = round($matched_count / $user_logged_tags_count,3)*100;  }
          if ($selected_user_tags_count>0) {
            $percent_his_view = round($matched_count / $selected_user_tags_count,3)*100  ;
           }
           
          if ($user_logged_in != $user->id) {
               
           echo "<div style='text-align: center; background-color: #EAF4FF; border: 1px double #000099';>" 
           . $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud_WithUser') .
           " <b> ".$user->username ." </b> ". $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud_Common') . " <b> " 
           .$percent . "% </b> " . $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud_OfYourTags'). ".<BR>".$main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud_UsersView').
           " <b>".$percent_his_view ."%</b> ". $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud_UsersViewOfTags')."</div>" ;
                    
                    }else {
            echo "<div style='text-align: center; background-color: #EAF4FF; border: 1px double #000099';>" 
           . $main_smarty->get_config_vars('PLIGG_Visual_User_TagCloud_ThisYour') . "</div>" ;
                    
                    
                    }  
          
                }       
            }       
              
   //******************************************************************************************** 
                
                  
         
  

    
 function do_stories () {
	global $db, $rows, $user, $offset, $page_size;
   if ($user->id!=6){ 	
	$link = new Link;
	$rows = $db->get_var("SELECT count(*) FROM " . table_saved_links . " WHERE saved_user_id=$user->id");
	$links = $db->get_col("SELECT saved_link_id FROM " . table_saved_links . " WHERE saved_user_id=$user->id ORDER BY saved_link_id DESC LIMIT $offset,$page_size");
	if ($links) {
		foreach($links as $link_id) {
			$link->id=$link_id;
			$link->read();
			$link->print_summary('summary');
		}
	}
}
}
function do_voted () {
	global $db, $rows, $user, $offset, $page_size;
     if ($user->id!=6){
	$link = new Link;
	$rows = $db->get_var("SELECT count(*) FROM " . table_links . ", " . table_votes . " WHERE vote_user_id=$user->id AND vote_link_id=link_id AND vote_value > 0 AND link_status!='discard' AND link_author NOT IN ($user->id)");
	$links = $db->get_col("SELECT DISTINCT link_id FROM " . table_links . ", " . table_votes . " WHERE vote_user_id=$user->id AND vote_link_id=link_id AND vote_value > 0  AND link_status!='discard' AND link_author NOT IN ($user->id) ORDER BY link_date DESC LIMIT $offset,$page_size");
	if ($links) {
		foreach($links as $link_id) {
			$link->id=$link_id;
			$link->read();
			$link->print_summary('summary');
		}
	}
  }
}
function do_history () {
	global $db, $rows, $user, $offset, $page_size;
     if ($user->id!=6){
	$link = new Link;
	$rows = $db->get_var("SELECT count(*) FROM " . table_links . " WHERE link_author=$user->id AND link_status!='discard'");
	$links = $db->get_col("SELECT link_id FROM " . table_links . " WHERE link_author=$user->id AND link_status!='discard' ORDER BY link_date DESC LIMIT $offset,$page_size");
	if ($links) {
		foreach($links as $link_id) {
			$link->id=$link_id;
			$link->read();
			$link->print_summary('summary');
		}
	}
}
}
function do_published () {
	global $db, $rows, $user, $offset, $page_size;
       if ($user->id!=6){ 
	$link = new Link;
	$rows = $db->get_var("SELECT count(*) FROM " . table_links . " WHERE link_author=$user->id AND link_status='published'");
	$links = $db->get_col("SELECT link_id FROM " . table_links . " WHERE link_author=$user->id AND link_status='published'  ORDER BY link_published_date DESC LIMIT $offset,$page_size");
	if ($links) {
		foreach($links as $link_id) {
			$link->id=$link_id;
			$link->read();
			$link->print_summary('summary');
		}
	}
}
 }
function do_shaken () {
	global $db, $rows, $user, $offset, $page_size;

	$link = new Link;
	$rows = $db->get_var("SELECT count(*) FROM " . table_links . " WHERE link_author=$user->id AND link_status='queued'");
	$links = $db->get_col("SELECT link_id FROM " . table_links . " WHERE link_author=$user->id AND link_status='queued' ORDER BY link_date DESC LIMIT $offset,$page_size");
	if ($links) {
		foreach($links as $link_id) {
			$link->id=$link_id;
			$link->read();
			$link->print_summary('summary');
		}
	}
}

function do_commented () {
  global $db, $rows, $user, $offset, $page_size;

  $link = new Link;
  $rows = $db->get_var("SELECT count(*) FROM " . table_links . ", " . table_comments . " WHERE comment_user_id=$user->id AND comment_link_id=link_id");
  $links = $db->get_col("SELECT DISTINCT link_id FROM " . table_links . ", " . table_comments . " WHERE comment_user_id=$user->id AND comment_link_id=link_id AND link_status != 'discard'  ORDER BY link_date DESC LIMIT $offset,$page_size");
  if ($links) {
    foreach($links as $link_id) {
      $link->id=$link_id;
      $link->read();
      $link->print_summary('summary');
    }
  }     
}

function do_removefriend (){
	global $db, $user, $the_template;
	$friend = new Friend;
	echo '<br /><center><span style="border:solid 1px #269900; padding: 2px 2px 2px 2px"><img src="'.my_pligg_base.'/templates/' . $the_template. '/images/green_check.gif" alt="Remove Friend Success" align="absmiddle" /> ';	
    $friend->remove($user->id);
	echo '</span></center>';
}

function do_addfriend (){
	global $db, $user, $the_template;
	$friend = new Friend;
	echo '<br /><center><span style="border:solid 1px #269900; padding: 2px 2px 2px 2px"><img src="'.my_pligg_base.'/templates/' . $the_template. '/images/green_check.gif" alt="Add Friend Success" align="absmiddle" /> ';
	$friend->add($user->id);
	echo '</span></center>';
}

function do_viewfriends(){
	global $db, $user, $the_template;
	$friend = new Friend;
	$friends = $friend->get_friend_list();

	echo "<h2>Vaši přátelé</h2>";	
    echo  '<img src="'.my_pligg_base.'/templates/' . $the_template. '/images/friend_big.png" style="width:32px;height:32px"/>';
	if ($friends){
	  echo '<table style=width:50%><th>Uživatelské jméno</th><th>Zpráva</th><th>Odstranit</th>';
		foreach($friends as $myfriend) {
			$friend_avatar = get_avatar('small', $myfriend->user_avatar_source, $myfriend->user_login, $myfriend->user_email);		    
			echo '<tr>';
			echo '<td><img src="'.$friend_avatar.'" align="absmiddle"/> <a href="'.getmyurl('user2', $myfriend->user_login, 'profile').'">' . $myfriend->user_login.'</a></td>';						
            echo '<td><a href="' . my_pligg_base . '/module.php?module=simple_messaging&view=compose&return=' . urlencode($_SERVER['REQUEST_URI']) .'&to=' .$myfriend->user_login . '"<img src='.my_pligg_base.'/templates/'.$the_template.'/images/user_message.png border=0></a></td>';			
			echo '<td><a href = "'.getmyurl('user_add_remove', $myfriend->user_login, 'removefriend').'"><img src='.my_pligg_base.'/templates/'.$the_template.'/images/user_delete.png border=0></a></td>';
			echo '</tr>';			
		}
		echo '</table>';
	}
	else {
		echo "<br /><br /><center><strong>nemáte žádné přátele</strong></center>";
	}
}

function do_viewfriends2(){
    global $db, $user, $the_template;
    $friend = new Friend;
    $friends = $friend->get_friend_list_2();    

    echo "<h2>Uživatelé kteří si vás označili jako přítele</h2>";
        echo  '<img src="'.my_pligg_base.'/templates/' . $the_template. '/images/friends2_big.png" style="width:32px;height:32px"/>';
        
    if ($friends){
      echo '<table style=width:50%><th>Uživatelské jméno</th><th>Zpráva</th>';
        foreach($friends as $myfriend) {
            $friend_avatar = get_avatar('small', $myfriend->user_avatar_source, $myfriend->user_login, $myfriend->user_email);
          echo '<tr>';
            echo '<td><img src="'.$friend_avatar.'" align="absmiddle"/> <a href="'.getmyurl('user2', $myfriend->user_login, 'profile'). '">' . $myfriend->user_login.'</a></td>';            
            echo '<td><a href="#view_message" rel="view_message~!~view=small_msg_compose~!~login=' .$myfriend->user_login . '" class="lbOn"><img src='.my_pligg_base.'/templates/'.$the_template.'/images/user_message.png border=0></a></td>';                    
            echo '</tr>';
        }
        echo '</table>';
    }
    else {
        echo "<br /><br /><center><strong>nikdo si vás neoznačil jako přítele (jak smutné)</strong></center>";
    }
}

?>